unit untMain_PD280;

interface

uses
  Windows, Messages, SysUtils, Forms, Dialogs, StdCtrls, ActnList, ExtDlgs, jpeg,
  Vcl.CheckLst, Vcl.ExtCtrls, Vcl.ComCtrls, untConstant, Vcl.ButtonGroup,
  Vcl.Controls, System.Classes;

type
  TOpenComm = function(sComm: AnsiString; iBaudRate: Integer): Boolean; stdcall;

  TCloseComm = procedure; stdcall;

  TLED_Display = function(sText: AnsiString; LightStatus: Integer): Integer; stdcall;

  TDSP_Init = function(): Integer; stdcall;

  TDSP_Restart = function(): Integer; stdcall;

  TDSP_GetFirmwareVersion = function(): Integer; stdcall;

  TDSP_UploadPicture = function(FileName: AnsiString; FilePathName: AnsiString): Integer; stdcall;

  TDSP_SetDefaultStyle = function(n: Integer): Integer; stdcall;

  TDSP_SwitchStyle = function(n: Integer): Integer; stdcall;

  TDSP_ShowQrCode_1 = function(pQRCodeText: AnsiString): Integer; stdcall;

  TSetDebugFlag = procedure(debug: Boolean); stdcall;

  TfrmMain_PD280 = class(TForm)
    btn_exit: TButton;
    grp_serial: TGroupBox;
    Label1: TLabel;
    btn_open: TButton;
    cbbPort: TComboBox;
    btn_close: TButton;
    btn_Refresh: TButton;
    OpenPic1: TOpenPictureDialog;
    btn_getfirmwareversion: TButton;
    btn_restart: TButton;
    GroupBox1: TGroupBox;
    mmo_barcodetext_style1: TMemo;
    btn_selectfile_style1: TButton;
    btn_uploadpicture_style1: TButton;
    btn_showbarcode_style1: TButton;
    lbl_barcodetext_style1: TLabel;
    edt_filename_style1: TEdit;
    cbb_fileno_style1: TComboBox;
    Label5: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure btn_openClick(Sender: TObject);
    procedure btn_closeClick(Sender: TObject);
    procedure btn_exitClick(Sender: TObject);
    procedure btn_RefreshClick(Sender: TObject);
    procedure btn_getfirmwareversionClick(Sender: TObject);
    procedure btn_restartClick(Sender: TObject);
    procedure btn_selectfile_style1Click(Sender: TObject);
    procedure btn_uploadpicture_style1Click(Sender: TObject);
    procedure btn_showbarcode_style1Click(Sender: TObject);
  private
    { Private declarations }
    IsOpened: Boolean;
    procedure ToggleEnabled;
  public
    { Public declarations }
  end;

var
  frmMain_PD280: TfrmMain_PD280;
  dllhandle: THandle;
  OpenComm: TOpenComm;
  CloseComm: TCloseComm;
  LED_Display: TLED_Display;
  DSP_Init: TDSP_Init;
  DSP_Restart: TDSP_Restart;
  DSP_GetFirmwareVersion: TDSP_GetFirmwareVersion;
  DSP_UploadPicture: TDSP_UploadPicture;
  DSP_SetDefaultStyle: TDSP_SetDefaultStyle;
  DSP_SwitchStyle: TDSP_SwitchStyle;
  DSP_ShowQrCode_1: TDSP_ShowQrCode_1;
  SetDebugFlag: TSetDebugFlag;

implementation

{$R *.dfm}

procedure TfrmMain_PD280.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  dllhandle := SafeLoadLibrary('TPD280DLL.dll');
  if (dllhandle <> 0) then
  begin
    @OpenComm := GetProcAddress(dllhandle, 'OpenComm');
    @CloseComm := GetProcAddress(dllhandle, 'CloseComm');

    @LED_Display := GetProcAddress(dllhandle, 'LED_Display');

    @DSP_Init := GetProcAddress(dllhandle, 'DSP_Init');
    @DSP_Restart := GetProcAddress(dllhandle, 'DSP_Restart');
    @DSP_GetFirmwareVersion := GetProcAddress(dllhandle, 'DSP_GetFirmwareVersion');

    @DSP_UploadPicture := GetProcAddress(dllhandle, 'DSP_UploadPicture');

    @DSP_SetDefaultStyle := GetProcAddress(dllhandle, 'DSP_SetDefaultStyle');
    @DSP_SwitchStyle := GetProcAddress(dllhandle, 'DSP_SwitchStyle');
    @DSP_ShowQrCode_1 := GetProcAddress(dllhandle, 'DSP_ShowQrCode_1');

    @SetDebugFlag := GetProcAddress(dllhandle, 'SetDebugFlag');
  end;

  cbbPort.Items.Clear;
  if EnumComPorts(cbbPort.Items) then
    cbbPort.ItemIndex := 0;

  //style 1
  edt_filename_style1.Text := '';
  cbb_fileno_style1.Items.Clear;
  for i := Low(UploadFileInfo_PD280_STYLE1) to High(UploadFileInfo_PD280_STYLE1) do
    cbb_fileno_style1.Items.Add(UploadFileInfo_PD280_STYLE1[i].Caption);
  cbb_fileno_style1.ItemIndex := 0;

  ToggleEnabled;
{$IFDEF DEBUG}
  SetDebugFlag(True);
  Caption := Caption + '    --debug--';
{$ENDIF}
end;

procedure TfrmMain_PD280.ToggleEnabled;
begin
  btn_open.Enabled := not IsOpened;
  btn_close.Enabled := IsOpened;

  //display
  btn_restart.Enabled := IsOpened;
  btn_getfirmwareversion.Enabled := IsOpened;

  //style 1
  cbb_fileno_style1.Enabled := IsOpened;
  edt_filename_style1.Enabled := IsOpened;
  mmo_barcodetext_style1.Enabled := IsOpened;
  btn_selectfile_style1.Enabled := IsOpened;
  btn_uploadpicture_style1.Enabled := IsOpened;
  btn_showbarcode_style1.Enabled := IsOpened;
end;

procedure TfrmMain_PD280.btn_openClick(Sender: TObject);
var
  iError: Integer;
begin
  if not OpenComm(AnsiString(cbbPort.Text), 115200) then
  begin
    ShowMessage('Error opening serial port');
    Exit;
  end;

  IsOpened := True;

  DSP_SetDefaultStyle(1);
  DSP_SwitchStyle(1);
  DSP_Init;

  ToggleEnabled;
end;

procedure TfrmMain_PD280.btn_RefreshClick(Sender: TObject);
begin
  if EnumComPorts(cbbPort.Items) then
    cbbPort.ItemIndex := 0;
end;

procedure TfrmMain_PD280.btn_restartClick(Sender: TObject);
begin
  DSP_Restart;
end;

procedure TfrmMain_PD280.btn_closeClick(Sender: TObject);
begin
  CloseComm;
  IsOpened := False;
  ToggleEnabled;
end;

procedure TfrmMain_PD280.btn_selectfile_style1Click(Sender: TObject);
begin
  OpenPic1.InitialDir := ExtractFilePath(Application.ExeName);
  if OpenPic1.Execute then
    edt_filename_style1.Text := OpenPic1.FileName;
end;

procedure TfrmMain_PD280.btn_showbarcode_style1Click(Sender: TObject);
var
  iError: Integer;
begin
  iError := DSP_ShowQrCode_1(UTF8Encode(mmo_barcodetext_style1.Text));
  ShowMessage(ExplainError(iError));
end;

procedure TfrmMain_PD280.btn_uploadpicture_style1Click(Sender: TObject);
var
  iError: Integer;
begin
  if FileExists(edt_filename_style1.Text) then
  begin
    iError := DSP_UploadPicture(
      PAnsiChar(AnsiString(UploadFileInfo_PD280_STYLE1[cbb_fileno_style1.ItemIndex].Name)),
      PAnsiChar(AnsiString(edt_filename_style1.Text)));
    ShowMessage(ExplainError(iError));
  end;
end;

procedure TfrmMain_PD280.btn_exitClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain_PD280.btn_getfirmwareversionClick(Sender: TObject);
var
  version: Integer;
begin
  version := DSP_GetFirmwareVersion;
  ShowMessage(IntToStr(version));
end;

end.

